package ld.servlet;

import com.alibaba.fastjson.JSON;
import ld.pojo.Emp;
import ld.pojo.PageResult;
import ld.service.EmpService;
import ld.service.impl.EmpServiceImpl;
import ld.vo.EmpVo;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.List;

/**
 * @Author: Liudao
 * @Date: 2022-08-01-22:08
 * @Description:
 */
@WebServlet("/emp")
public class EmpServlet extends HttpServlet {
    private EmpService empService;

    @Override
    public void init() throws ServletException {
        empService = new EmpServiceImpl();
    }

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //处理中文乱码
        req.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html;charset=utf-8");
        //得到用于请求的参数
        String cmd = req.getParameter("cmd");
        //根据请求分发方法
        if (StringUtils.isNotBlank(cmd)) {
            if("list".equals(cmd)){         //列表学生
                list(req,resp);
            }
            if ("search".equals(cmd)) {       //条件查询带分页
                search(req, resp);
            }
            if ("add".equals(cmd)) {          // 添加员工
                add(req, resp);
            }
            if ("update".equals(cmd)) {       // 修改员工
                update(req, resp);
            }
            if ("delete".equals(cmd)) {       // 删除员工
                delete(req, resp);
            }
        }
    }

    private void list(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        List<Emp> empList = empService.findAll();
        //转换为json串并输出
        resp.getWriter().print(JSON.toJSONString(empList));
        resp.getWriter().flush();
    }


    //分页条件查询
    private void search(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        //得到分页参数
        String pageStr = req.getParameter("page");
        String pagesizeStr = req.getParameter("pagesize");
        //转换为int并提供默认值
        int page = StringUtils.isNotBlank(pageStr) ? Integer.parseInt(pageStr) : 1;
        int pagesize = StringUtils.isNotBlank(pagesizeStr) ? Integer.parseInt(pagesizeStr) : 5;
        //得到查询对象
        String empSearch = req.getParameter("empSearch");
        empSearch = new String(empSearch.getBytes(StandardCharsets.UTF_8));
        EmpVo vo = null;
        if (StringUtils.isNotBlank(empSearch)){
            vo = JSON.parseObject(empSearch,EmpVo.class);
        }
        //开始查询
        PageResult<Emp> pr = empService.search(page, pagesize, vo);
        //转换为字符串
        resp.getWriter().println(pr);
        resp.getWriter().flush();

    }

    //添加
    private boolean add(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        try {
            //得到前端传入的数据
            String emp = req.getParameter("emp");
            emp = new String(emp.getBytes(StandardCharsets.UTF_8));
            //转换为emp对象
            Emp emp1 = JSON.parseObject(emp, Emp.class);
            //添加到数据库并返回
            return empService.add(emp1);
        }catch (Exception e){
            e.printStackTrace();
        }
        resp.getWriter().println(false);
        resp.getWriter().flush();
        return false;
    }

    //修改
    private boolean update(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        try {
            //得到前端传入的数据
            String emp = req.getParameter("emp");
            emp = new String(emp.getBytes(StandardCharsets.UTF_8));
            //转换为emp对象
            Emp emp1 = JSON.parseObject(emp, Emp.class);
            //从数据库修改并返回
            return empService.update(emp1);
        }catch (Exception e){
            e.printStackTrace();
        }
        resp.getWriter().println(false);
        resp.getWriter().flush();
        return false;
    }

    //删除
    private boolean delete(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        try {
            //得到前端传入的数据
            String[] eids = req.getParameter("eids").split(",");
            //删除并返回
            return empService.delete(eids);
        }catch (Exception e){
            e.printStackTrace();
        }
        resp.getWriter().println(false);
        resp.getWriter().flush();
        return false;
    }

}